package org.pay.manage.config.ctrl;

import com.alibaba.fastjson.JSONObject;
import org.pay.manage.common.ctrl.BaseController;
import org.pay.manage.common.service.RpcCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.pay.core.common.constant.Constant;
import org.pay.core.common.constant.MchConstant;
import org.pay.core.common.constant.RetEnum;
import org.pay.core.common.domain.BizResponse;
import org.pay.core.common.domain.PayPageRes;
import org.pay.core.common.domain.PayResponse;
import org.pay.core.common.util.ObjectValidUtil;
import org.pay.core.entity.ChannelConfig;
import org.pay.core.entity.PayChannel;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author: yangshancheng
 * @date: 18/01/17
 * @description:
 */
@RestController
@RequestMapping(Constant.MGR_CONTROLLER_ROOT_PATH + "/config/pay_channel")
@PreAuthorize("hasRole('"+ MchConstant.MGR_ROLE_NORMAL+"')")
public class PayChannelController extends BaseController {

    @Autowired
    private RpcCommonService rpcCommonService;

    @RequestMapping("/list")
    @ResponseBody
    public ResponseEntity<?> list(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Integer passageId = getIntegerRequired(param, "passageId");
        Byte scene = param.getByte("scene");
        List<PayChannel> payChannelList = rpcCommonService.rpcPayChannelService.selectByPassageId(passageId, scene);
        return ResponseEntity.ok(PayResponse.buildSuccess(payChannelList));
    }

    /**
     * 查询渠道信息
     * @return
     */
    @RequestMapping("/get")
    @ResponseBody
    public ResponseEntity<?> get(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Integer passageId = getIntegerRequired(param, "passageId");
        String channelId = getStringRequired(param, "channelId");
        PayChannel payChannel = rpcCommonService.rpcPayChannelService.findByPassageAndChannelId(passageId, channelId, null);
        if(payChannel == null) {
            ChannelConfig channelConfig = rpcCommonService.rpcChannelConfigService.findByChannelId(channelId);
            if(channelConfig == null) ResponseEntity.ok(PayResponse.buildSuccess(null));
            payChannel = new PayChannel();
            payChannel.setPassageId(passageId);
            payChannel.setChannelId(channelConfig.getChannelId());
            payChannel.setChannelType(channelConfig.getChannelType());
            payChannel.setScene(channelConfig.getScene());
            payChannel.setState(MchConstant.PUB_NO);   // 默认未开通
            payChannel.setParam(channelConfig.getParam());
            payChannel.setRemark(channelConfig.getRemark());
        }
        return ResponseEntity.ok(PayResponse.buildSuccess(payChannel, param));
    }

    @RequestMapping("/update")
    @ResponseBody
    public ResponseEntity<?> update(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Integer id = param.getInteger("id");    // 支付渠道ID
        Integer passageId = getIntegerRequired(param, "passageId");
        String channelId = getStringRequired(param, "channelId");
        String channelMchId = param.getString("channelMchId");
        BigDecimal channelRate = param.getBigDecimal("channelRate");
        Byte status = param.getByte("state");
        String param1 = param.getString("param");
        String remark = param.getString("remark");
        int count;
        // 主键不为空,则为修改
        if(id != null) {
            PayChannel payChannel = new PayChannel();
            payChannel.setId(id);
            payChannel.setPassageId(passageId);
            if(channelMchId != null) payChannel.setChannelMchId(channelMchId);
            if(channelRate != null) payChannel.setChannelRate(channelRate);
            if(status != null) payChannel.setState(status);
            if(param1 != null) payChannel.setParam(param1);
            if(remark != null) payChannel.setRemark(remark);
            count = rpcCommonService.rpcPayChannelService.update(payChannel);
        }else {
            if(!ObjectValidUtil.isValid(param)) {
                return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_PARAM_ERROR));
            }
            ChannelConfig channelConfig = rpcCommonService.rpcChannelConfigService.findByChannelId(channelId);
            if(channelConfig == null) ResponseEntity.ok(BizResponse.build(RetEnum.RET_MCH_CHANNEL_NOT_EXIST));
            PayChannel payChannel = new PayChannel();
            payChannel.setPassageId(passageId);
            payChannel.setChannelId(channelId);
            payChannel.setChannelType(channelConfig.getChannelType());
            payChannel.setScene(channelConfig.getScene());
            payChannel.setChannelMchId(channelMchId);
            payChannel.setChannelRate(channelRate);
            payChannel.setState(status);
            payChannel.setParam(param1);
            payChannel.setRemark(channelConfig.getRemark());
            count = rpcCommonService.rpcPayChannelService.add(payChannel);
        }
        if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }
}
